//获取当前dom节点下的所有元素子节点，方法就是获取当前dom节点下的所有子节点，
//去掉文本节点和其他节点，只保留元素节点
var utils = (function(){
    //判断是否在ie678浏览器下,true就是在标准浏览器下，false就是在ie678下
    var navigato = "getComputedStyle" in window;
    console.log(navigato); 
    //获取节点所有的儿子元素节点
    function children(curEle,tarName){
        
        if(/MSIE (6|7|8)/i.test(navigator.userAgent)){
            //这是在ie678下使用的方法
            var list = curEle.childNodes;
            var ary = [];
            for(var i = 0; i < list.length;i++){
                if(list[i].nodeType === 1 ){
                    ary.push(list[i]);
                }
            }
        }else{
            //在标准浏览器下可以使用slice.call方法把类数组curEle.children转化为数组。
            ary = Array.prototype.slice.call(curEle.children);
        }
        //二次筛选,找到符合tarName标签名的元素
        if(typeof tarName === "string"){
            for(i = 0; i<ary.length;i++){
                if(ary[i].nodeName.toLowerCase() != tarName.toLowerCase()){
                    ary.splice(i,1);
                    i--;
                }
            }
        }
        
        return ary;
}
    //寻找上一个哥哥￥元素节点$(与之相邻)
    function prev(curEle){
        if(navigato === true){
            return curEle.previousElementSibling;
        }else{
            var pre = curEle.previousSibling;
            while(pre && pre.nodeType != 1){
                pre = pre.previousSibling;
            }

            return pre;

            
        }
    }
    //寻找所有哥哥元素节点
    function prevAll(curEle) {
        var ary = [];
        var pre = prev(curEle);
        while(pre){
            ary.unshift(pre);
            pre = prev(pre);
        }

        return ary;


    }
    //寻找下一个弟弟元素节点
    function next(curEle){
        if(navigato === true){
            return curEle.nextElementSibling;
        }else{
            var next = curEle.nextSibling;
            while(next && next.nodeType != 1){
                next = next.nextSibling;
            }

            return next;
        }
    }

    //寻找所有弟弟元素节点
    function nextAll(curEle){
        var ary = [];
        var next = this.next(curEle);
        while(next){
            ary.push(next);
            next = this.next(next);
        }

        return ary;
    }
    
    //insertBefore
    function insertBefore(newEle,oldEle){
         oldEle.parentNode.insertBefore(newEle,oldEle);

    }

    //insertAfter
    function insertAfter(newEle,oldEle){
        var fid = next(oldEle);
        if(fid){
            insertBefore(newEle,fid);
            return;
        }

        oldEle.parentNode.appendChild(newEle);


    }

    //添加某个样式

function addClass(curEle,newClass){
    var ary = newClass.split(/ +/g);
    
    for(var i = 0; i < ary.length; i++ ){
        if(!hasClass(curEle,ary[i])){
            curEle.className += (" " + ary[i]);
        }
    }
    console.log(curEle.className);
}

//  <div class="box position clear border">

//判断curEle元素节点的样式类名中是否有classname这个样式

function hasClass(curEle,classname){
    var reg = new RegExp("( +|^)" + classname + "( +|$)");
    
    return reg.test(curEle.className);


}

//去除某个样式

function removeClass(curEle,classname){
    var ary = classname.split(/ +/g);
    console.log(ary);
    for(var i = 0; i < ary.length; i++ ){
        if(hasClass(curEle,ary[i])){
            var reg = new RegExp("( +|^)" + ary[i] + "( +|$)");
            
            curEle.className = curEle.className.replace(reg," ");
        }
    }
}

function getElementsByClassname(classname,context){
    context = context || document;
    if(navigato == false){
        var arr  =[];
    //如果context为undefined则context = document.
    
    var NodeList = context.getElementsByTagName('*');
    console.log(NodeList);
    //
    var ary = classname.replace(/(^ +| +$)/g,'').split(/ +/);
    console.log(ary);
    
    for(var i = 0; i < NodeList.length; i++){
        var isOk = true;
        var curNode = NodeList[i];
        for(var j = 0; j < ary.length; j++){
            var curClass = ary[j];
            var reg = new RegExp('(^| +)' + curClass + '($| +)');
            if(!reg.test(curNode.className)){
                isOk = false;
                break;
            }
        }

        if(isOk){
            arr.push(curNode);
        }
        
        
    }

    return arr;
    }else{//在标准浏览器下
        return Array.prototype.slice.call(context.getElementsByClassName(classname));
    }
    
}
    return{
        children:children,
        prev:prev,
        next:next,
        prevAll:prevAll,
        nextAll:nextAll,
        addClass:addClass,
        hasClass:hasClass,
        removeClass:removeClass,
        getElementsByClassname:getElementsByClassname
    }
})()
    

